using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ArchSoft
{
    public partial class WatcherFrm : Form
    {

        private delegate void de_GhiData();
        DataTable dt = new DataTable();
        DataRow row;

        string _name;
        string _fullPath;
        string _type;

        string _username;

        public string UserName
        {
            set { _username = value; }
            get { return _username; }
        }

        public WatcherFrm()
        {
            InitializeComponent();

            dt.Columns.Add("Name");
            dt.Columns.Add("Element");
            dt.Columns.Add("Time");
            dt.Columns.Add("Type");
            dt.Columns.Add("UserName");


            dataGridView1.DataSource = dt;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            
        }

        FileSystemWatcher fsw;

        private string _projectPath;

        public string ProjectPath
        {
            set { _projectPath = value; }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                this.button1.Enabled = true;
                this.button2.Enabled = false;
                this.button3.Enabled = true;
                this.button4.Enabled = true;
                this.button5.Enabled = false;

                this.button7.Enabled = false;
                this.button8.Enabled = false;

                textBox1.Text = _projectPath;

                string path = _projectPath + Path.DirectorySeparatorChar + "Log.scr";
                DataTable dt1 = new DataTable();
                DataSet ds1 = new DataSet();
                if (File.Exists(path))
                {
                    ds1.ReadXml(path);
                    dt1 = ds1.Tables[0];
                    dt = dt1.Copy();
                }

                dataGridView1.DataSource = dt;
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            {

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            fsw = new FileSystemWatcher();
            fsw.Path = textBox1.Text;
            fsw.Filter = "*.*";
            fsw.IncludeSubdirectories = true;
            fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                     | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size;
            fsw.Changed +=new FileSystemEventHandler(onFileEvent);
            fsw.Created += new FileSystemEventHandler(onFileEvent);
            fsw.Deleted += new FileSystemEventHandler(onFileEvent);
            fsw.Renamed += new RenamedEventHandler(onRenameEvent);
            fsw.EnableRaisingEvents = true;

            button2.Enabled = true;
            button1.Enabled = false;
            button4.Enabled = true;

            button5.Enabled = true;
            button7.Enabled = true;
            button8.Enabled = true;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog of = new FolderBrowserDialog();
            if (of.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = of.SelectedPath;
            }

            button1.Enabled = true;
        }

        private void onFileEvent(object sender, FileSystemEventArgs e)
        {
            try
            {
                
                string[] eName = e.Name.Split(Path.DirectorySeparatorChar);
                _name = eName[eName.Length - 1].ToString();

                //
                if (_name == "Log.scr")
                {
                    return;
                }

                //
                string[] ePath = e.FullPath.Split(Path.DirectorySeparatorChar);
                _fullPath = ePath[ePath.Length - 2].ToString();
                _type = e.ChangeType.ToString();

                de_GhiData d1 = GhiData;
                this.Invoke(d1);
            }
            catch (Exception ex)
            {

            }
            //row = dt.NewRow();
            //row["Name"] = e.Name;
            //row["Path"] = e.FullPath;
            //row["Time"] = DateTime.Now.ToLongDateString();
            //row["Type"] = e.ChangeType.ToString();
            //dt.Rows.Add(row);



            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    ListViewItem item = new ListViewItem();
            //    ListViewItem.ListViewSubItem item1 = new ListViewItem.ListViewSubItem();
            //    ListViewItem.ListViewSubItem item2 = new ListViewItem.ListViewSubItem();
            //    ListViewItem.ListViewSubItem item3 = new ListViewItem.ListViewSubItem();
            //    ListViewItem.ListViewSubItem item4 = new ListViewItem.ListViewSubItem();

            //    item.Text = dt.Rows[i]["Name"].ToString();
            //    item2.Text = dt.Rows[i]["Path"].ToString();
            //    item3.Text = dt.Rows[i]["Time"].ToString();
            //    item4.Text = dt.Rows[i]["Type"].ToString();

            //    item.SubItems.AddRange(new ListViewItem.ListViewSubItem[] { item2, item3, item4 });
            //    listView1.Items.Add(item);
            //}
            
        }

        private void GhiData()
        {
            try
            {

                row = dt.NewRow();
                row["Name"] = _name;
                row["Element"] = _fullPath;
                row["Time"] = DateTime.Now.ToLocalTime();
                row["Type"] = _type;
                row["UserName"] = _username;
                dt.Rows.Add(row);

                //for (int i = 0; i < dt.Rows.Count; i++)
                //{
                //    ListViewItem item = new ListViewItem();
                //    ListViewItem.ListViewSubItem item1 = new ListViewItem.ListViewSubItem();
                //    ListViewItem.ListViewSubItem item2 = new ListViewItem.ListViewSubItem();
                //    ListViewItem.ListViewSubItem item3 = new ListViewItem.ListViewSubItem();
                //    ListViewItem.ListViewSubItem item4 = new ListViewItem.ListViewSubItem();

                //    item.Text = dt.Rows[i]["Name"].ToString();
                //    item2.Text = dt.Rows[i]["Path"].ToString();
                //    item3.Text = dt.Rows[i]["Time"].ToString();
                //    item4.Text = dt.Rows[i]["Type"].ToString();

                //    item.SubItems.AddRange(new ListViewItem.ListViewSubItem[] { item2, item3, item4 });
                //    listView1.Items.Add(item);
                //}


                dataGridView1.DataSource = dt;
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            {

            }
        }

        private void onRenameEvent(object sender, RenamedEventArgs e)
        {
            //row = dt.NewRow();
            //row["Name"] = e.Name;
            //row["Path"] = e.FullPath;
            //row["Time"] = DateTime.Now.ToLongDateString();
            //row["Type"] = "Rename from" + e.OldName; 
            //dt.Rows.Add(row);

            //ViewList();
            try
            {
                
                string[] eName = e.Name.Split(Path.DirectorySeparatorChar);
                _name = eName[eName.Length - 1].ToString();
                string[] ePath = e.FullPath.Split(Path.DirectorySeparatorChar);
                _fullPath = ePath[ePath.Length - 2].ToString();

                // _type = "Rename from " + e.OldName;
                string[] eName1 = e.OldName.Split(Path.DirectorySeparatorChar);
                _type = "Rename from " + eName1[eName.Length - 1].ToString();
                de_GhiData d1 = GhiData;
                this.Invoke(d1);
            }
            catch (Exception ex)
            {

            }
        }

        private void ViewList()
        {
            dataGridView1.DataSource = dt;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            fsw.EnableRaisingEvents = false;
            button1.Enabled = true;
            button2.Enabled = false;
            button4.Enabled = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            dt.Rows.Clear();
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }

        private void restoreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //Save
            if (b_Save)
            {
                this.Close();

            }
            else
            {
                string path = _projectPath + Path.DirectorySeparatorChar + "Log.scr";
                //
                //DataTable dt1 = new DataTable();
                //DataSet ds1 = new DataSet();
                //if (File.Exists(path))
                //{
                //    ds1.ReadXml(path);
                //    dt1 = ds1.Tables[0];

                //    if (dt1.Rows.Count > 0)
                //    {
                //        for (int i = 0; i < dt1.Rows.Count; i++)
                //        {
                //            row = dt.NewRow();
                //            row["Name"] = dt1.Rows[i]["Name"].ToString();
                //            row["Element"] = dt1.Rows[i]["Element"].ToString();
                //            row["Time"] = dt1.Rows[i]["Time"].ToString();
                //            row["Type"] = dt1.Rows[i]["Type"].ToString();
                //            row["UserName"] = _username;
                //            dt.Rows.Add(row);
                //        }
                //    }
                //}
                if (dt.Rows.Count > 0)
                {
                    DataSet ds = new DataSet();
                    //
                    DataTable temp = dt.Copy();
                    ds.Tables.Add(temp);
                    ds.WriteXml(path);
                    //
                }
                this.Close();
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                string path = _projectPath + Path.DirectorySeparatorChar + "Log.scr";
                //
                DataTable dt1 = new DataTable();
                DataSet ds1 = new DataSet();
                if (File.Exists(path))
                {
                    ds1.ReadXml(path);
                    dt1 = ds1.Tables[0];

                    if (dt1.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt1.Rows.Count; i++)
                        {
                            row = dt.NewRow();
                            row["Name"] = dt1.Rows[i]["Name"].ToString();
                            row["Element"] = dt1.Rows[i]["Element"].ToString();
                            row["Time"] = dt1.Rows[i]["Time"].ToString();
                            row["Type"] = dt1.Rows[i]["Type"].ToString();
                            row["UserName"] = _username;
                            dt.Rows.Add(row);
                        }
                    }
                }
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                ds.WriteXml(path);

                //fsw.EnableRaisingEvents = false;
                
            }
            catch (Exception ex)
            {

            }

        }

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                string path = _projectPath + Path.DirectorySeparatorChar + "Log.scr";
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                dt.Rows.Clear();
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            //ViewList();
            //fsw.EnableRaisingEvents = false;
            }
            catch (Exception ex)
            {

            }
        }

        bool b_Save = false;

        private void button9_Click(object sender, EventArgs e)
        {
            string path = _projectPath + Path.DirectorySeparatorChar + "Log.scr";
            /////

            //DataTable dt1 = new DataTable();
            //DataSet ds1 = new DataSet();
            //if (File.Exists(path))
            //{
            //    ds1.ReadXml(path);
            //    dt1 = ds1.Tables[0];

            //    if (dt1.Rows.Count > 0)
            //    {
            //        for (int i = 0; i < dt1.Rows.Count; i++)
            //        {
            //            row = dt.NewRow();
            //            row["Name"] = dt1.Rows[i]["Name"].ToString();
            //            row["Element"] = dt1.Rows[i]["Element"].ToString();
            //            row["Time"] = dt1.Rows[i]["Time"].ToString();
            //            row["Type"] = dt1.Rows[i]["Type"].ToString();
            //            row["UserName"] = _username;
            //            dt.Rows.Add(row);
            //        }
            //    }
            //}
            DataSet ds = new DataSet();
            //
            DataTable temp = dt.Copy();
            ds.Tables.Add(temp);
            ds.WriteXml(path);
            //

            b_Save = true;
            ////
            WatcherReport frm = new WatcherReport();
            frm.Project = path;
            frm.LoadReport();
            frm.WindowState = FormWindowState.Maximized;
            frm.ShowDialog();
        }
    }
}